package com.skycong.hello77.tabfragment.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.skycong.hello77.R;
import com.skycong.hello77.tabfragment.adapter.TabFragmentAdapter;
import com.skycong.hello77.tabfragment.fragment.MyFooterFragment;
import com.skycong.hello77.tabfragment.fragment.SignFragment;
import com.skycong.hello77.tabfragment.viewmodel.TabFragmentViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 点击tab切换，比如首页的四个界面
 * Created by Kiki on 2018/6/21.
 */

public class TabFragmentActivity extends FragmentActivity implements View.OnClickListener {
    private Context context;
    private TabFragmentViewModel viewModel;
    private SignFragment signFragment;//签到
    private MyFooterFragment myFooterFragment;//我的足迹
    private int selectPage = FragmentType.签到.type;
    private List<Fragment> viewList;//Tab页面列表
    private TabFragmentAdapter viewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_tab_fragment);
        initView();
        initViewPager();
    }

    private void initView() {
        context = this;
        viewModel = new TabFragmentViewModel(context);
        viewModel.setListener(this);
        viewModel.setSelected(selectPage);
    }

    private void initViewPager() {
        if(myFooterFragment == null){
            myFooterFragment = new MyFooterFragment();
        }
        viewList = new ArrayList<>();
        viewList.add(myFooterFragment);
        if(signFragment == null){
            signFragment = new SignFragment();
        }
        viewList.add(signFragment);
        viewAdapter = new TabFragmentAdapter(getSupportFragmentManager(),viewList,this);
        viewModel.viewpager.setAdapter(viewAdapter);
        viewModel.viewpager.addOnPageChangeListener(new MyOnPageChangeListener());
        viewModel.viewpager.setOffscreenPageLimit(1);//限定预加载的页面个数
        viewModel.viewpager.setScrollAble(true);//可左右滑动
        setCurrentItem(selectPage,false);

    }
    /**
     * tab切换
     *tab切换，能左右滚动切换
     * @param page
     * @param smoothScroll
     */
    public void setCurrentItem(int page, boolean smoothScroll) {
        viewModel.viewpager.setCurrentItem(page, smoothScroll);
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.layout_signin) {//签到
            selectPage = FragmentType.签到.type;
            setCurrentItem(selectPage, false);
        } else if (id == R.id.layout_footprint) {//足迹
            selectPage = FragmentType.我的足迹.type;
            setCurrentItem(selectPage, false);
        }

    }
     public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // arg0 ==1的时候表示正在滑动，arg0==2的时候表示滑动完毕了，arg0==0的时候表示什么都没做，就是停在那。

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // 表示在前一个页面滑动到后一个页面的时候，在前一个页面滑动前调用的方法。

        }

        @Override
        public void onPageSelected(int position) {
            // arg0是表示你当前选中的页面，这事件是在你页面跳转完毕的时候调用的。
            selectPage = position;
            //页面刷新
            viewModel.setSelected(selectPage);

        }
    }

    public enum  FragmentType{
             签到(0),
            我的足迹(1);
       public int type;
        FragmentType(int type){
            this.type = type;
        }
    }
}
